<?php
class RoleModule extends DBModel {
	private $table = 'admin_role';

	public function __construct($db_choose = 'default'){
		parent::__construct($db_choose);
	}

	public function getAllRole($adminid=0){
		$where = '';
		if($adminid){
			$where = ' and admin_id=:adminid';
		}
//		$sql = 'select role_id,role_name,`desc`,`status`,FROM_UNIXTIME(r.createdtime) createdtime,FROM_UNIXTIME(lastmodifiedtime) lastmodifiedtime from ' . $this->table . '  where ' . $where;
        $sql = "select role_id,role_name,`desc`,`status`,FROM_UNIXTIME(createdtime) createdtime,FROM_UNIXTIME(lastmodifiedtime) lastmodifiedtime from {$this->table} where 1=1 {$where}";
		$this->_db->prepare($sql);
		if($where){
			$this->_db->bindValue(':adminid', $adminid, PDO::PARAM_INT);
		}
		$this->_db->execute();
		return $this->_db->fetchAll(PDO::FETCH_ASSOC);
	}

	/**
	 * 通过角色ＩＤ，得到角色信息
	 *
	 * @param mixed $roleid        	
	 * @param mixed $fileds        	
	 */
	public function getInfoByRoleID($roleid, $fileds = ''){
		if(! AppCommon::intParse($roleid, $roleid) && $roleid < 0){
			return null;
		}
		
		if(empty($fileds)){
			$fileds = 'role_id,role_name,`desc`,`status`';
		}
		
		$sql = "select {$fileds} from {$this->table} where role_id=:roleid";
		$this->_db->prepare($sql);
		$this->_db->bindValue(":roleid", $roleid, PDO::PARAM_INT);
		
		if($this->_db->execute()){
			return $this->_db->fetch(PDO::FETCH_ASSOC);
		}
		
		return null;
	}


	/**
	 * 通过角色名查询角色信息
	 *
	 * @param mixed $rolename        	
	 */
	public function getInfoByRoleName($rolename){
		if(empty($rolename)){
			return null;
		}
		
		$sql = "select role_id,role_name,`desc`,`status` from {$this->table} where role_name=:role_name limit 0,1";
		$this->_db->prepare($sql);
		$this->_db->bindValue(":role_name", $rolename, PDO::PARAM_STR);
		
		if($this->_db->execute()){
			return $this->_db->fetch(PDO::FETCH_ASSOC);
		}
		
		return null;
	}

	/**
	 * 增加角色
	 *
	 * @param mixed $name        	
	 * @param mixed $desc        	
	 * @param mixed $status        	
	 */
	public function addRole($name, $desc, $status, $adminid){
		if(empty($name) || empty($status) || empty($adminid)){
			return false;
		}
		
		$sql = "insert into {$this->table} (`role_name`,`desc`,`admin_id`,`status`,`createdtime`,`lastmodifiedtime`) values (:name,:desc,:adminid,:status,:createdtime,:lastmodifiedtime)";
		$this->_db->prepare($sql);
		$this->_db->bindValue(":name", $name, PDO::PARAM_STR);
		$this->_db->bindValue(":desc", $desc, PDO::PARAM_STR);
		$this->_db->bindValue(":adminid", $adminid, PDO::PARAM_INT);
		$this->_db->bindValue(":status", $status, PDO::PARAM_INT);
		$this->_db->bindValue(":createdtime", time(), PDO::PARAM_INT);
		$this->_db->bindValue(":lastmodifiedtime", time(), PDO::PARAM_INT);
		if($this->_db->execute()){
			return $this->_db->rowCount();
		}
		
		return false;
	}

	/**
	 * 修改角色信息
	 *
	 * @param mixed $roleid        	
	 * @param mixed $name        	
	 * @param mixed $desc        	
	 * @param mixed $status        	
	 */
	public function editRole($roleid, $name, $desc, $status){
		if(! AppCommon::intParse($roleid, $roleid) || $roleid <= 0 || empty($name) || ! is_numeric($status)){
			return false;
		}
		
		$sql = "update {$this->table} set `role_name`=:name,`desc`=:desc,`status`=:status,`lastmodifiedtime`=:lastmodifiedtime where role_id=:role_id";
		$this->_db->prepare($sql);
		$this->_db->bindValue(":name", $name, PDO::PARAM_STR);
		$this->_db->bindValue(":desc", $desc, PDO::PARAM_STR);
		$this->_db->bindValue(":status", $status, PDO::PARAM_INT);
		$this->_db->bindValue(":lastmodifiedtime", time(), PDO::PARAM_INT);
		$this->_db->bindValue(":role_id", $roleid, PDO::PARAM_INT);
		if($this->_db->execute()){
			return $this->_db->rowCount();
		}
		
		return false;
	}

	/**
	 * 修改角色权限
	 *
	 * @param mixed $roleid        	
	 * @param mixed $purview        	
	 */
	public function editRolePower($roleid, $purview){
		if(! AppCommon::intParse($roleid, $roleid) || $roleid <= 0){
			return false;
		}
		
		$sql = "update {$this->table} set `priv`=:purview where role_id=:role_id";
		$this->_db->prepare($sql);
		
		$this->_db->bindValue(":purview", $purview, PDO::PARAM_STR);
		$this->_db->bindValue(":role_id", $roleid, PDO::PARAM_INT);
		if($this->_db->execute()){
			return $this->_db->rowCount();
		}
		
		return false;
	}

	/**
	 * 删除角色
	 *
	 * @param mixed $userid        	
	 */
	public function deleteRole($userid){
		if(! AppCommon::intParse($userid, $roleid) || $roleid <= 0){
			return false;
		}
		
		$sql = "delete from {$this->table}  where role_id=:role_id";
		$this->_db->prepare($sql);
		$this->_db->bindValue(":role_id", $roleid, PDO::PARAM_INT);
		
		if($this->_db->execute()){
			return $this->_db->rowCount();
		}
		
		return false;
	}

	/**
	 * 查询所有角色列表
	 * 
	 * @return multitype:array
	 */
	public function getSelectRole(){
		$data = array();
		$sql = 'select role_id,role_name from ' . $this->table;
		
		$this->_db->query($sql);
		while($row = $this->_db->fetch(PDO::FETCH_ASSOC)){
			$data[$row['role_id']] = $row;
		}
		return $data;
	}

	public function getRolePurview($roles){
		if(empty($roles)){
			return;
		}
		$pri_arr = array();
		$roleid = is_array($roles) ? implode('","', $roles) : $roles;
		
		$sql = 'select priv from ' . $this->table . ' where role_id in("' . $roleid . '")';
		
		$this->_db->query($sql);
		while($row = $this->_db->fetch(PDO::FETCH_ASSOC)){
			$pri_arr = $row['priv'] ? array_merge($pri_arr, unserialize($row['priv'])) : $pri_arr;
		}
		
		return $pri_arr;
	}

}
